<?php
/**
 * 订单模块后台 Service
 *
 * 订单模块后台 Service的注意及说明
 * 以下use模块请不要互相伤害
 *
 * @author    xiecz <xiecz@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Services\Order;
use Log;
use App\Services\Tool\DataBase\SQL;
use App\Services\Tool\GeneralTool;
use App\Services\Tool\Order\OrderTool;
use App\Services\Tool\ExpressTool;
use App\Services\Tool\Order\AfterTool;
use App\Services\Tool\Zip;

class AfterBackstage{

    function __construct(OrderTool $orderTool,
                         ExpressTool $express,
                         AfterTool $afterTool,
                         Zip $zip){
        $this->orderTool = $orderTool;
        $this->express   = $express;
        $this->afterTool = $afterTool;
        $this->zip       = $zip;
    }

    /**
        订单列表
        参数：
            order_id    订单号         order_aftermarks
            state       订单状态       order_aftermarks
            time_range  下单时间范围    order_aftermarks
     */
    function aftermark_list($input){
        $arr = [];
        foreach($input as $k=>$v){
            if($k == 'state' || $k == 'order_id' || $k == 'time_range')
                $arr[$k] = $v;
        }

        $orders = $this->afterTool->aftermark_lists_backstage($arr);
        $orders->appends($input)->render();

        return $orders;
    }

    /**
        售后详情
        参数：
            order_after_id      售后单号
     */
    function aftermark_detail($input){
        if(!isset($input['order_after_id']))
            return (object)[];

        $after = $this->afterTool->aftermark_detail($input);//dd($after);
        return $after;
    }

    /**
        修改售后审核结果
        参数：
            order_after_id      售后单号
            state               结果状态0未通过1已通过4官方已收货
     */
    function update_after_state($input){
        if(!isset($input['order_after_id']) || !isset($input['state']))
            return GenralTool::createResult('','M00002');

        $result = $this->afterTool->update_after_state($input);
        return GeneralTool::createResult('',$result);
    }

    /**
        修改售后审核描述
        参数：
            order_after_id      售后单号
            result_desc         审核描述
     */
    function update_result_desc($input){
        if(!isset($input['order_after_id']) || !isset($input['result_desc']))
            return GeneralTool::createResult('','M00002');
        if(trim($input['result_desc']) == '')
            return GeneralTool::createResult('','M05057');
        $input['result_desc'] = bin2hex($input['result_desc']);
        $result = SQL::update(['table' => 'order_aftermarks',
                               'where' => ['order_after_id' => $input['order_after_id']],
                               'update'=> ['result_desc'    => $input['result_desc']]]);
        return GeneralTool::createResult('',($result ? 'M00000' : 'M00001'));
    }

    /**
        售后单重新发货
        参数：
            order_after_id          售后单号
            send_express_id         物流公司代码
            send_order_express_id   快递单号
     */
    function system_resend_express($input){
        if(!isset($input['order_after_id']) ||
            !isset($input['send_express_id']) ||
            !isset($input['send_order_express_id'])||
            trim($input['order_after_id']) == '' ||
            trim($input['send_express_id']) == '' ||
            trim($input['send_order_express_id']) == '')
            return GeneralTool::createResult('','M00002');
        $result = $this->afterTool->system_resend_express($input);

        if($result == 'M00000'){
            //订阅物流信息
            $re = $this->express->subscribeExpressInfo($input['send_express_id'],$input['send_order_express_id']);
        }
        return GeneralTool::createResult('', $result);
    }

    /**
        修改售后单后台备注信息
        参数：
            order_after_id          售后单号
            back_remark             填写售后单备注
     */
    function update_aftermark_back_remark($input){
        if(!isset($input['order_after_id']) || !isset($input['back_remark']))
            return GeneralTool::createResult('','M00002');

        $input['back_remark'] = trim($input['back_remark']) != '' ? bin2hex($input['back_remark']) : '';
        $result = $this->afterTool->update_aftermark_back_remark($input);
        return GeneralTool::createResult('',$result);
    }

    /**
        打包售后单打印图
     */
    function create_after_zip($input){
        if(!isset($input['order_after_id'])) return GeneralTool::createResult('','M00002');
        $data = $this->afterTool->get_after_prints_to_zip($input['order_after_id']);

        $result = $this->zip->createZip(array('zip_name'=>($input['order_after_id'].'.zip'),
                                              'type'=>'order',
                                              'file'=>$data));

        if(isset($result['state'])&&$result['state']=='00'){
            return GeneralTool::createResult($result,'M00000');
        }
        return ['state'=>'M00001','message'=>$result['msg'],'result'=>$result];
    }

    function test($input){
        return GeneralTool::createResult('','M00000');
    }
}
